package com.myjob.system.controller;

import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.myjob.system.utils.OssUtil;
import com.shopgoods.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.shopgoods.common.log.annotation.Log;
import com.shopgoods.common.log.enums.BusinessType;
import com.shopgoods.common.security.annotation.RequiresPermissions;
import com.myjob.system.domain.Brand;
import com.myjob.system.service.IBrandService;
import com.shopgoods.common.core.web.controller.BaseController;
import com.shopgoods.common.core.web.domain.AjaxResult;
import com.shopgoods.common.core.utils.poi.ExcelUtil;
import com.shopgoods.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 品牌Controller
 *
 * @author shopgoods
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController
{
    @Autowired
    private IBrandService brandService;
    @Autowired
    private OssUtil ossUtil;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 查询品牌列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Brand brand)
    {
        // 尝试从Redis中获取缓存的文章列表
        List range = redisTemplate.opsForList().range("List", 0, -1);
        // 删除Redis中的缓存列表，以准备存储最新的文章列表
        redisTemplate.delete("List");
        // 检查从Redis中获取的数据是否为空如果为空，表示需要从数据库中获取数据
        if (range != null && range.size() > 0) {
            return getDataTable(range);
        }
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        // 将查询到的文章列表添加到Redis缓存中
        redisTemplate.opsForList().rightPush("List", list);
        return getDataTable(list);
    }

    /**
     * 获取品牌详细信息
     */
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@PathVariable("brandId") Integer brandId)
    {
        return success(brandService.selectBrandByBrandId(brandId));
    }

    /**
     * 新增或修改品牌
     */
    @Log(title = "品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated Brand brand)
    {
        boolean b = brandService.saveOrUpdate(brand);
        if (b==true){
            redisTemplate.delete("List");
            return toAjax(b);
        }
        return toAjax(b);
    }

//    /**
//     * 修改品牌
//     */
//    @Log(title = "品牌", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody Brand brand)
//    {
//        return toAjax(brandService.updateBrand(brand));
//    }

    /**
     * 删除品牌
     */
    @Log(title = "品牌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{brandIds}")
    public AjaxResult removeByIds(@PathVariable Integer[] brandIds)
    {

        boolean b = brandService.removeBatchByIds(Arrays.asList(brandIds));
        if (b==true) {
            redisTemplate.delete("List");
            return toAjax(b);
        }
        return toAjax(b);
    }

//    /**
//     * 删除品牌
//     */
//    @Log(title = "品牌", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{brandId}")
//    public AjaxResult removeById(@PathVariable Integer brandId)
//    {
//        boolean b = brandService.removeById(brandId);
//        if (b==true) {
//            redisTemplate.delete("List");
//            return toAjax(b);
//        }
//        return toAjax(b);
//    }


    /**
     * 图片上传
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile myPic){
        String upload = ossUtil.uploadMultipartFile(myPic);
        return new AjaxResult().success(upload);
    }


    /**
     * 修改品牌显示状态是否显示
     */
    @PutMapping("/updateBrandStatusData")
    public AjaxResult updateBrandStatusData(@RequestBody Brand brand)
    {
        int i = brandService.updateBrandStatusData(brand);
        if (i>0){
            redisTemplate.delete("List");
            return toAjax(i);
        }
        return AjaxResult.error();
    }
}
